package com.itheima.api;

import com.itheima.tanhua.api.mongo.CommentApi;
import com.itheima.tanhua.enums.CommentType;
import com.itheima.tanhua.pojo.mongo.Comment;
import com.itheima.tanhua.pojo.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

/**
 * @author 芝士雪豹
 * @create 2022/9/25/17:19
 */
@DubboService
public class CommentApiImpl implements CommentApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    //保存发布评论
    @Override
    public Integer saveComments(Comment comments) {
        //获取movement对象 =动态对象
        Movement movement = mongoTemplate.findById(comments.getPublishId(), Movement.class);
        //向comment对象设置被评论人属性
        if (movement != null) {
            comments.setPublishUserId(comments.getPublishUserId());
        }
        //保存到数据库
        mongoTemplate.save(comments);
        //更新动态表的对应字段
        //创建更新条件
        Query query = Query.query(Criteria.where("id").is(comments.getPublishUserId()));
        Update update = new Update();
        if (comments.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else if (comments.getCommentType() == CommentType.LOVE.getType()) {
            update.inc("loveCount", 1);
        } else {
            update.inc("commentCount", 1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions().returnNew(true);
        //获取最新的评论数量,返回
        Movement movement1 = mongoTemplate.findAndModify(query, update, options, Movement.class);
        return movement1.statisCount(comments.getCommentType());
    }


    //查询是否点赞了  comment数据是否存在
    @Override
    public boolean findIsNotLike(String movementId, Long id, CommentType like) {
        Query query = Query.query(Criteria.where("userId").is(id)
                .and("publishId").is(movementId)
                .and("comment").is(like.getType()));
        boolean exists = mongoTemplate.exists(query, Comment.class);
        return exists;
    }
    //取消点赞
    @Override
    public Integer remove(Comment comment) {
        Query query = Query.query(Criteria.where("userId").is(comment.getId())
                .and("publishId").is(comment.getPublishUserId())
                .and("comment").is(comment.getCommentType()));
        mongoTemplate.remove(query, Comment.class);

        Query disquery = Query.query(Criteria.where("id").is(comment.getPublishUserId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else if (comment.getCommentType() == CommentType.LOVE.getType()) {
            update.inc("loveCount", -1);
        } else {
            update.inc("commentCount", -1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions().returnNew(true);
        //获取最新的评论数量,返回
        Movement movement1 = mongoTemplate.findAndModify(disquery, update, options, Movement.class);

        return movement1.statisCount(comment.getCommentType());
    }


}
